package com.it.chinamobileai.controller;

import com.it.chinamobileai.domain.vo.MessageVO;
import com.it.chinamobileai.service.IChatHistoryService;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    @Autowired
    private ChatMemoryRepository chatMemoryRepository;
    @Autowired
    private ChatMemory chatMemory;
    @Autowired
    private IChatHistoryService chatHistoryService;

    @GetMapping("/{type}")
    public List<String> getChat1(@PathVariable("type") String type){
//        return chatMemoryRepository.findConversationIds();
        return chatHistoryService.getChatIds(type,null);
    }
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChat2(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
//        List<Message> byConversationId = chatMemoryRepository.findByConversationId(chatId);
//        List<MessageVO> list = byConversationId.stream().map(message -> {
//            return new MessageVO(message);
//        }).toList();
        List<Message> messageList = chatMemory.get(chatId);
        return messageList.stream().map(message -> new MessageVO(message)).toList();
    }
}
